<#include "/lib/lib.html">
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>用户管理</title>
    <meta name="keywords" content="">
    <meta name="description" content="">

    <link rel="shortcut icon" href="${request.contextPath}/favicon.ico">
    <link href="${request.contextPath}/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="${request.contextPath}/css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="${request.contextPath}/css/animate.css" rel="stylesheet">
    <link href="${request.contextPath}/css/style.css?v=4.1.0" rel="stylesheet">
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.min.css">
    <link rel="stylesheet" href="${request.contextPath}/css/plugins/bootstrap-validator/bootstrapValidator.min.css">
    <link href="${request.contextPath}/css/plugins/iCheck/custom.css" rel="stylesheet">
    <!-- Latest compiled and minified CSS -->
    <style>
        .form-control:focus,
        .single-line:focus {
            border-color: #0e9aef !important;
        }
        .checkbox-inline {
            line-height: 5px;
        }
    </style>
</head>
<body class="gray-bg">

    <div class="wrapper wrapper-content">

        <div class="ibox float-e-margins ibox-blue">
            <div class="ibox-title">
                <h5>用户管理</h5>
                <div class="ibox-tools">
                    <a class="collapse-link">
                        <i class="fa fa-chevron-up"></i>
                    </a>
                    <a class="close-link">
                        <i class="fa fa-times"></i>
                    </a>
                </div>
            </div>
            <div class="ibox-content">
                <div class="row row-lg">

                    <div class="example" style="margin-top: -10px;">
                        <div class="btn-group hidden-xs" id="toolbar" role="group">
                            <button type="button" class="btn btn-outline btn-default" data-toggle="modal" data-target="#addModal">
                                <i class="glyphicon glyphicon-plus" aria-hidden="true"></i>
                                添加
                            </button>
                            <button type="button" class="btn btn-outline btn-default" id="updateButton">
                                <i class="glyphicon glyphicon-pencil" aria-hidden="true"></i>
                                修改
                            </button>
                            <button type="button" class="btn btn-outline btn-default" id="distribution">
                                <i class="glyphicon glyphicon-edit" aria-hidden="true"></i>
                                分配角色
                            </button>
                            <button type="button" class="btn btn-outline btn-default" id="optionUserStatus">
                                <i class="glyphicon glyphicon-edit" aria-hidden="true"></i>
                                审核通过
                            </button>
                            <button type="button" class="btn btn-outline btn-default" id="deleteButton">
                                <i class="glyphicon glyphicon-trash" aria-hidden="true"></i>
                                删除
                            </button>
                        </div>
                        <table id="userTableEvents"></table>
                    </div>

                </div>
            </div>
        </div>

    </div>

    <@modal "添加用户" "addModal" "addForm">
        <div class="form-group">
            <label class="col-sm-2 control-label">用户名</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="username" placeholder="请输入用户名">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">昵称</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="nickname" placeholder="请输入昵称">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">密码</label>
            <div class="col-sm-10">
                <input type="password" class="form-control" name="password" autocomplete="off" placeholder="请输入密码">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">确认密码</label>
            <div class="col-sm-10">
                <input type="password" class="form-control" name="confirmPassword" autocomplete="off" placeholder="请输入确认密码">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">邮箱</label>
            <div class="col-sm-10">
                <input type="email" class="form-control" name="email" placeholder="请输入邮箱">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">状态</label>
            <div class="col-sm-10">
                <div class="ui-radio ui-radio-primary">
                    <label>
                        <input type="radio" name="status" value="1" checked="">
                        <span>启用</span>
                    </label>
                    <label>
                        <input type="radio" name="status" value="0">
                        <span>禁用</span>
                    </label>
                </div>
            </div>
        </div>
    </@modal>

    <@modal "修改用户" "updateModal" "updateForm">
        <input type="hidden" name="id">
        <div class="form-group">
            <label class="col-sm-2 control-label">用户名</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="username" placeholder="请输入用户名">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">昵称</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="nickname" placeholder="请输入昵称">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">邮箱</label>
            <div class="col-sm-10">
                <input type="email" class="form-control" name="email" placeholder="请输入邮箱">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">状态</label>
            <div class="col-sm-10">
                <div class="ui-radio ui-radio-primary">
                    <label>
                        <input type="radio" name="status" value="1">
                        <span>启用</span>
                    </label>
                    <label>
                        <input type="radio" name="status" value="0">
                        <span>禁用</span>
                    </label>
                </div>
            </div>
        </div>
    </@modal>

    <@modal "分配角色" "distributionModal" "distributioneForm">
        <#list allRole as item>
            <label class="checkbox-inline i-checks">
            <input type="checkbox" name="roleId" value="${item.id}"> ${item.name}
            </label>
        </#list>
    </@modal>

    <!-- 全局js -->
    <script src="${request.contextPath}/js/jquery.min.js?v=2.1.4"></script>
    <script src="${request.contextPath}/js/bootstrap.min.js?v=3.3.6"></script>
    <!-- layer javascript -->
    <script src="${request.contextPath}/js/plugins/layer/layer.min.js"></script>
    <!-- 自定义js -->
    <script src="${request.contextPath}/js/content.js?v=1.0.0"></script>
    <!-- Latest compiled and minified JavaScript -->
    <script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.min.js"></script>
    <!-- Latest compiled and minified Locales -->
    <script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/locale/bootstrap-table-zh-CN.min.js"></script>
    <script src="${request.contextPath}/js/plugins/bootstrap-validator/bootstrapValidator.js"></script>
    <script src="${request.contextPath}/js/plugins/iCheck/icheck.min.js"></script>

    <script>
        $(document).ready(function () {
            $('.i-checks').iCheck({
                checkboxClass: 'icheckbox_square-green',
                radioClass: 'iradio_square-green',
            });
        });
        $("#addUserModal").modal();
        $(function () {
            $("#userTableEvents").bootstrapTable({
                url: '/admin/user/getList',
                method: 'post',
                contentType: "application/x-www-form-urlencoded",
                queryParamsType:'',//查询参数组织方式
                queryParams:queryParams,//请求服务器时所传的参数
                sidePagination:'server',//指定服务器端分页
                search: true,
                searchOnEnterKey: true,
                pagination: true,
                showRefresh: true,
                showToggle: true,
                showColumns: true,
                striped: true,
                pagination: true,
                sortable: false,
                sortOrder: "asc",
                clickToSelect: true,
                pageNumber:1,
                pageSize: 10,
                pageList: [1, 5, 10, 25, 50, 100, 'ALL'],
                iconSize: 'outline',
                toolbar: '#toolbar',
                icons: {
                    refresh: 'glyphicon-repeat',
                    toggle: 'glyphicon-list-alt',
                    columns: 'glyphicon-list'
                },
                responseHandler:function(res){
                    //在ajax获取到数据，渲染表格之前，修改数据源
                    /*console.log(res)*/
                    return res;
                },
                columns: [
                    {
                        title:'全选',
                        field:'select',
                        //复选框
                        checkbox:true,
                        width:25,
                        align:'center',
                        valign:'middle'
                    },
                    {
                        field: 'id',
                        title: 'ID',
                        align: 'center'
                    },
                    {
                        field: 'username',
                        title: '用户名',
                        align: 'center'
                    },
                    {
                        field: 'nickname',
                        title: '昵称',
                        align: 'center'
                    },
                    {
                        field: 'email',
                        title: '邮箱',
                        align: 'center'
                    },
                    {
                        field: 'status',
                        title: '状态',
                        align: 'center',
                        formatter: function(value,row,index){
                            if (value == 0) {
                                return '<span class="label label-danger">禁用</span>';
                            } else if (value == 1) {
                                return '<span class="label label-primary">启用</span>';
                            }
                            else if (value == 5) {
                                return '<span class="label label-primary">待审核</span>';
                            }
                            return "其他";
                        }
                    },
                    {
                        field: 'gmtCreate',
                        title: '创建时间',
                        align: 'center',
                        formatter:function(value,row,index){
                            return formatDateTime(value);
                        }
                    },
/*                    {
                        field: 'id',
                        title: '操作',
                        align: 'center',
                        formatter:function(value,row,index){
                            //通过formatter可以自定义列显示的内容
                            //value：当前field的值，即id
                            //row：当前行的数据
                            var a = '<a href="" >测试</a>';
                            return a + value;
                        }
                    },*/
                ]
            });

            //请求服务数据时所传参数
            function queryParams(params){
                return{
                    pageNumber: params.pageNumber,
                    pageSize: params.pageSize,
                    searchText: params.searchText
                }
            }

            //添加用户
            $('#addModal').on('show.bs.modal', function () {
                $("#addForm").data('bootstrapValidator').destroy();
                $('#addForm').data('bootstrapValidator', null);
                addValidator();
            });
            addValidator();
            function addValidator() {
                $('#addForm').bootstrapValidator({
                    fields: {
                        username: {
                            validators: {
                                notEmpty: {
                                    message: '用户名不能为空'
                                },
                                stringLength: {
                                    max: 30,
                                    message: '用户名长度最多30个字符'
                                },
                                /*remote: {
                                    url: 'remote.php',
                                    message: 'The username is not available'
                                },*/
                            }
                        },
                        nickname: {
                            validators: {
                                notEmpty: {
                                    message: '昵称不能为空'
                                },
                                stringLength: {
                                    max: 30,
                                    message: '昵称长度最多30个字符'
                                },
                                /*remote: {
                                    url: 'remote.php',
                                    message: 'The username is not available'
                                },*/
                            }
                        },
                        email: {
                            validators: {
                                notEmpty: {
                                    message: '邮箱不能为空'
                                },
                                emailAddress: {
                                    message: '请输入有效的邮箱'
                                }
                            }
                        },
                        password: {
                            validators: {
                                notEmpty: {
                                    message: '密码不能为空'
                                },
                                stringLength: {
                                    min: 5,
                                    max: 30,
                                    message: '密码长度在5到30之间'
                                }
                            }
                        },
                        confirmPassword: {
                            validators: {
                                notEmpty: {
                                    message: '确认密码不能为空'
                                },
                                identical: {
                                    field: 'password',
                                    message: '两次密码不相同'
                                }
                            }
                        }
                    }
                }).on('success.form.bv', function(e) {
                    // Prevent form submission
                    e.preventDefault();
                    $.ajax({
                        url: "/admin/user/addUser",
                        data:  $("#addForm").serialize(),
                        type: "POST",
                        dataType: 'json',
                        success: function(data){
                            $("#addModal").modal('hide');
                            if (data.success) {
                                $("#addModal").modal('hide');
                                $("#addForm")[0].reset();
                                $('#userTableEvents').bootstrapTable('refresh');
                                parent.layer.msg(data.msg, {icon: 1});
                            } else {
                                parent.layer.msg(data.msg, {time: 1500, icon:5});
                            }
                        }
                    });
                });
            }

            //修改用户
            $("#updateButton").click(function () {
                var $result = $('#userTableEvents');
                var list = $result.bootstrapTable('getSelections');
                if (list.length <= 0 || list.length > 1) {
                    parent.layer.msg('请选中一条数据', {time: 1500, icon:5});
                    return;
                }
                list = list[0];
                for (var item in list) {
                    if (item == 'status') {
                        continue;
                    }
                    $("#updateModal input[name='"+item+"']").val(list[item]);
                }
                $("#updateModal input[name='status'][value="+list.status+"]").prop("checked",true);
                $("#updateForm").data('bootstrapValidator').destroy();
                $('#updateForm').data('bootstrapValidator', null);
                updateValidator();
                $("#updateModal").modal("show");
            });
            updateValidator();
            function updateValidator() {
                $('#updateForm').bootstrapValidator({
                    fields: {
                        username: {
                            validators: {
                                notEmpty: {
                                    message: '用户名不能为空'
                                },
                                stringLength: {
                                    max: 30,
                                    message: '用户名长度最多30个字符'
                                },
                                /*remote: {
                                    url: 'remote.php',
                                    message: 'The username is not available'
                                },*/
                            }
                        },
                        nickname: {
                            validators: {
                                notEmpty: {
                                    message: '昵称不能为空'
                                },
                                stringLength: {
                                    max: 30,
                                    message: '昵称长度最多30个字符'
                                },
                                /*remote: {
                                    url: 'remote.php',
                                    message: 'The username is not available'
                                },*/
                            }
                        },
                        email: {
                            validators: {
                                notEmpty: {
                                    message: '邮箱不能为空'
                                },
                                emailAddress: {
                                    message: '请输入有效的邮箱'
                                }
                            }
                        }
                    }
                }).on('success.form.bv', function(e) {
                    // Prevent form submission
                    e.preventDefault();
                    $.ajax({
                        url: "/admin/user/updateUser",
                        data:  $("#updateForm").serialize(),
                        type: "POST",
                        dataType: 'json',
                        success: function(data){
                            $("#updateModal").modal('hide');
                            if (data.success) {
                                $("#updateModal").modal('hide');
                                $('#userTableEvents').bootstrapTable('refresh');
                                parent.layer.msg(data.msg, {icon: 1});
                            } else {
                                parent.layer.msg(data.msg, {time: 1500, icon:5});
                            }
                        }
                    });
                });
            }

            $("#optionUserStatus").click(function () {
                var $result = $('#userTableEvents');
                var list = $result.bootstrapTable('getSelections');
                if (list.length <= 0) {
                    parent.layer.msg('请至少选中一条数据', {time: 1500, icon:5});
                    return;
                }

                for (var item in list) {
                    if(list[item].status != 5 ) {
                        parent.layer.msg('请选择待审核用户', {time: 1500, icon:5});
                        return;
                    }
                }

                $.ajax({
                    url: "/admin/user/optionUserStatus",
                    data:  JSON.stringify(list),
                    contentType: "application/json; charset=utf-8",
                    type: "POST",
                    dataType: 'json',
                    success: function(data){
                        if (data.success) {
                            $('#userTableEvents').bootstrapTable('refresh');
                            parent.layer.msg(data.msg, {icon: 1});
                        } else {
                            parent.layer.msg(data.msg, {time: 1500, icon:5});
                        }
                    }
                });
            });

            //删除用户
            $("#deleteButton").click(function () {
                var $result = $('#userTableEvents');
                var list = $result.bootstrapTable('getSelections');
                if (list.length <= 0) {
                    parent.layer.msg('请至少选中一条数据', {time: 1500, icon:5});
                    return;
                }
                var id = "";
                for (var item in list) {
                    id += list[item].id+",";
                }
                parent.layer.confirm('确定删除选中用户？', {
                    btn: ['确定','取消'], //按钮
                    shade: false //不显示遮罩
                }, function(){
                    $.ajax({
                        url: "/admin/user/delete",
                        data:  {"id": id},
                        type: "POST",
                        dataType: 'json',
                        success: function(data){
                            if (data.success) {
                                $('#userTableEvents').bootstrapTable('refresh');
                                parent.layer.msg(data.msg, {icon: 1});
                            } else {
                                parent.layer.msg(data.msg, {time: 1500, icon:5});
                            }
                        }
                    });
                });
            });

            //分配角色
            $("#distribution").click(function () {
                $("#distributionModal input[name='roleId']").iCheck('uncheck');
                var $result = $('#userTableEvents');
                var list = $result.bootstrapTable('getSelections');
                if (list.length <= 0 || list.length > 1) {
                    parent.layer.msg('请选中一条数据', {time: 1500, icon:5});
                    return;
                }
                list = list[0];
                $.ajax({
                    url: "/admin/role/getUserRole",
                    data:  JSON.stringify(list),
                    contentType: "application/json; charset=utf-8",
                    type: "POST",
                    dataType: 'json',
                    success: function(data){
                        if (data.success) {
                            var userRoles = data.obj;
                            for (var content in userRoles) {
                                $("#distributionModal input[name='roleId'][value='"+ userRoles[content].rid+"']").iCheck('check');
                            }
                        }
                    }
                });

                $("#distributionModal").modal('show');
            });
            $("#distributioneForm").submit(function (e) {
                e.preventDefault();
                // 选中用户
                var $result = $('#userTableEvents');
                var list = $result.bootstrapTable('getSelections');
                list = list[0];

                //选中角色
                var userRole = [];
                var roleId = [];
                $("#distributionModal input[name='roleId']:checked").each(function(){
                    userRole.push({"uid": list.id, 'rid': $(this).val()})
                    roleId.push({'rid': $(this).val()})
                })

                if (roleId.length <= 0) {
                    parent.layer.msg('请选择角色', {time: 1500, icon:5});
                    return;
                }

                $.ajax({
                    url: "/admin/role/modifyUserRole",
                    data:  JSON.stringify(userRole),
                    contentType: "application/json; charset=utf-8",
                    type: "POST",
                    dataType: 'json',
                    success: function(data){
                        if (data.success) {
                            $("#distributionModal").modal('hide');
                            parent.layer.msg(data.msg, {icon: 1});
                        } else {
                            parent.layer.msg(data.msg, {time: 1500, icon:5});
                        }
                    }
                });
            });

            /*$('.search').click(function(){
                $('#userTableEvents').bootstrapTable('refresh');
            })*/
        });
    </script>
</body>
</html>
